package Junit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.lang.StringUtils;

public class CountDownLatchs implements Runnable{
	final AtomicInteger number = new AtomicInteger();  
    volatile boolean bol = false;
    int i = 0;
    @Override  
    public void run() {  
        System.out.println(number.getAndIncrement());  
        synchronized (this) {  
            try {  
                if (!bol) {  
                    // System.out.println(bol);  
                    bol = true;  
                    Thread.sleep(10000);  
                }  
            } catch (InterruptedException e) {  
                e.printStackTrace();  
            }
            System.out.println(i++ + ": 并发数量为" + number.intValue());
            loadurl();
        }  
  
    }
    
	public void loadurl(){
    	String url = "http://nbuilder.celerstar.com";
    	// String url1 = "http://app.znllm.com/app/userCenter/getIdentifyingCode?mobileNumber=18801185080";
    	String str = doGet(url, "", "utf-8", true);
    	System.out.println(str);
    }
    
    public static String doGet(String url, String queryString, String charset,
			boolean pretty) {
		StringBuffer response = new StringBuffer();
		HttpClient client = new HttpClient();
		HttpMethod method = new GetMethod(url);
		try {
			if (StringUtils.isNotBlank(queryString))
				// 对get请求参数做了http请求默认编码，好像没有任何问题，汉字编码后，就成为%式样的字符串
				method.setQueryString(URIUtil.encodeQuery(queryString));
			client.executeMethod(method);
			// System.out.println(method.getStatusCode());
			// if (method.getStatusCode() == HttpStatus.SC_OK) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					method.getResponseBodyAsStream(), charset));
			String line;
			while ((line = reader.readLine()) != null) {
				if (pretty)
					response.append(line).append(System.getProperty("line.separator"));
				else
					response.append(line);
			}
			reader.close();
			// }
		} catch (URIException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}
		return response.toString();
	}
  
    public static void main(String[] args) {  
        ExecutorService pool = Executors.newCachedThreadPool();  
        CountDownLatchs test = new CountDownLatchs();  
        for (int i=0;i<2000;i++) {  
            pool.execute(test);  
        }
        pool.shutdown();
    }  
}
